<?php
/**
 * Created by PhpStorm.
 * User: john
 * Date: 2016/11/21
 * Time: 12:46
 */

namespace base;


class Xhprof
{
    /**
     * 是否已经开启开始的开关(防止重复开始或者未开始就输出)
     * @var bool
     */
    private static $_is_run = false;

    /**
     * 调试开始点
     * @return bool
     */
    public static function start()
    {
        if (static::$_is_run) {
            return false;
        }
        xhprof_enable(XHPROF_FLAGS_NO_BUILTINS | XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY);
        static::$_is_run = true;
    }

    /**
     * 调试输出点
     * @param string $key
     * @return bool
     */
    public static function displayDebug($key = 'xhprof')
    {

        if (!static::$_is_run) {
            return false;
        }
        static::$_is_run = false;
        $xhprof_data     = xhprof_disable();    //结束,然后写入文件
        $XHPROF_ROOT     = realpath(dirname(__FILE__) . '/..');
        include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
        include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";

        $xhprof_runs = new \XHProfRuns_Default();
        $run_id      = $xhprof_runs->save_run($xhprof_data, $key);
        return $run_id;
    }


}